/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Forms;

import BLL.BLLNguoiDung;
import BLL.BLLNhanVien;
import DAL.DALNguoiDung;
import DTO.DTONguoiDung;
import DTO.DTONhanVien;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Documents
 */
public class ThemNguoiDung extends javax.swing.JDialog {
    BLLNhanVien bllNV = new BLLNhanVien();
    BLLNguoiDung bllNgDung = new BLLNguoiDung();
    /**
     * Creates new form ThemNguoiDung
     */
    public ThemNguoiDung() {
        initComponents();
        DefaultComboBoxModel modelNV = new DefaultComboBoxModel();
        ArrayList<DTONhanVien> dsNV = bllNV.dsNhanVien();
        for(DTONhanVien dtoNV : dsNV){
            modelNV.addElement(dtoNV);
        }   
        jcboxNV.setModel(modelNV);//truyen doi tuong vao combobox
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jtxtDangNhap = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        jtxtEmail = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jtxtMk = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jtxtNhacMk = new javax.swing.JPasswordField();
        jbtnLuu = new javax.swing.JButton();
        jbtnHuy = new javax.swing.JButton();
        jcboxNV = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 102, 0));
        jLabel2.setText("THÊM NGƯỜI DÙNG");

        jLabel5.setText("Nhân viên");

        jLabel15.setText("Tên đăng nhập");

        jtxtDangNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtDangNhapActionPerformed(evt);
            }
        });

        jLabel16.setText("Email");

        jLabel1.setText("Mật khẩu");

        jtxtMk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtxtMkActionPerformed(evt);
            }
        });

        jLabel6.setText("Nhập lại mật khẩu");

        jbtnLuu.setText("Lưu");
        jbtnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLuuActionPerformed(evt);
            }
        });

        jbtnHuy.setText("Đóng");
        jbtnHuy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnHuyActionPerformed(evt);
            }
        });

        jcboxNV.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcboxNV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcboxNVActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel15)
                            .addComponent(jLabel1)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel16))
                        .addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jcboxNV, 0, 231, Short.MAX_VALUE)
                            .addComponent(jtxtNhacMk, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtMk, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtDangNhap, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtEmail))))
                .addContainerGap(24, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtnLuu)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnHuy)
                .addGap(36, 36, 36))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(jtxtDangNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxtMk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtNhacMk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcboxNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(jtxtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnLuu)
                    .addComponent(jbtnHuy))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jtxtDangNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtDangNhapActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtDangNhapActionPerformed

    private void jcboxNVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcboxNVActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcboxNVActionPerformed

    //Luu
    private void jbtnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLuuActionPerformed
        if("".equals(kiemtra())){
            DTONhanVien dtoNV = (DTONhanVien)jcboxNV.getSelectedItem();
            int manv = dtoNV.getMaNV();
            String tendn=jtxtDangNhap.getText().trim();
            String mk = jtxtMk.getText().trim();
            String mail = jtxtEmail.getText().trim();
            DTONguoiDung dtoNgDung = new DTONguoiDung();
            dtoNgDung.setTenDN(tendn);
            dtoNgDung.setMatKhau(mk);
            dtoNgDung.setEmail(mail);
            dtoNgDung.setDtoNV(dtoNV);
            if(bllNgDung.LayNgDungTheoTen(tendn) != null)
                JOptionPane.showMessageDialog(null, "Tên người dùng đã tôn tại");
            else
            {
                if(bllNgDung.ThemNguoiDung(dtoNgDung))
                     JOptionPane.showMessageDialog(null, "Đã thêm người dùng");
            }
        }
        else{
            JOptionPane.showMessageDialog(null, kiemtra());
        }
        
    }//GEN-LAST:event_jbtnLuuActionPerformed

    //kiem tra nhap
    private String kiemtra(){
        String kq="";
        if("".equals(jtxtDangNhap.getText().trim()))
            kq += "Vui lòng nhập tên\n";
        if("".equals(jtxtMk.getText().trim()))
            kq += "Vui lòng nhập mật khẩu\n";
        if("".equals(jtxtNhacMk.getText().trim()))
            kq += "Vui lòng nhắc lại mật khẩu\n";
        if(!jtxtNhacMk.getText().equals(jtxtMk.getText()))
            kq += "Mật khẩu nhắc lại không đúng\n";
        String emailreg = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        if(!jtxtEmail.getText().trim().matches(emailreg))
            kq += "Địa chỉ mail không hợp lệ\n";
        if("".equals(jtxtEmail.getText().trim()))
            kq += "Vui lòng nhập email\n";
        return kq;
    }
    
    private void jtxtMkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtxtMkActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jtxtMkActionPerformed

    private void jbtnHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnHuyActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbtnHuyActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ThemNguoiDung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ThemNguoiDung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ThemNguoiDung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThemNguoiDung.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ThemNguoiDung().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton jbtnHuy;
    private javax.swing.JButton jbtnLuu;
    private javax.swing.JComboBox jcboxNV;
    private javax.swing.JTextField jtxtDangNhap;
    private javax.swing.JTextField jtxtEmail;
    private javax.swing.JPasswordField jtxtMk;
    private javax.swing.JPasswordField jtxtNhacMk;
    // End of variables declaration//GEN-END:variables
}
